#include <iostream>
#include <chrono>

// 使用 std::chrono 来记录时间
float myCPUTimer() {
    static auto last_time = std::chrono::high_resolution_clock::now();
    auto current_time = std::chrono::high_resolution_clock::now();
    std::chrono::duration<float, std::milli> duration = current_time - last_time;
    last_time = current_time;
    return duration.count();
}

int main() {

    // Image

    int image_width = 256;
    int image_height = 256;

    float t1 = myCPUTimer();
    // Render

    std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";

    for (int j = 0; j < image_height; j++) {
        for (int i = 0; i < image_width; i++) {
            auto r = double(i) / (image_width-1);
            auto g = double(j) / (image_height-1);
            auto b = 0.0;

            //使用255.999进行乘法的目的是确保当浮点数为1.0或接近1.0时，乘积总是足够接近255，而且即使有微小的浮点误差，也足够被四舍五入到255。
            int ir = int(255.999 * r);
            int ig = int(255.999 * g);
            int ib = int(255.999 * b);

            std::cout << ir << ' ' << ig << ' ' << ib << '\n';
        }
    }
    float t2 = myCPUTimer();
    std::cout << "Cpu execution time: " << t2 - t1 << " ms" << std::endl;
}